package com.carrot.api.controller.system;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.carrot.common.core.controller.BaseController;
import com.carrot.common.core.domain.AjaxResult;
import com.carrot.common.core.domain.dto.PointEquipmentDto;
import com.carrot.common.core.domain.entity.SysPointEquipment;
import com.carrot.common.core.domain.entity.SysStation;
import com.carrot.common.core.domain.vo.SysPointEquipmentVo;
import com.carrot.common.exception.ServiceException;
import com.carrot.common.mybatis.Page;
import com.carrot.common.utils.StringUtils;
import com.carrot.common.utils.WrapperUtil;
import com.carrot.system.service.ISysPointEquipmentService;
import com.carrot.system.service.ISysStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 点表工具
 *
 * @author liuhongzhen
 */
@RestController
@RequestMapping("/system/point-equipment")
public class SysPointEquipmentController extends BaseController {

    @Autowired
    private ISysPointEquipmentService pointEquipmentService;
    @Autowired
    private ISysStationService stationService;

    /**
     * 设备信息
     *
     * @param dto
     * @return
     */
    @PostMapping("list")
    public AjaxResult list(@RequestBody PointEquipmentDto dto) {

        Page<SysPointEquipment, SysPointEquipmentVo> page = new Page<>(dto.getPageNum(), dto.getPageSize());

        QueryWrapper<SysPointEquipment> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(dto.getSearchContent())) {
            queryWrapper.like("station_id", dto.getSearchContent())
                    .or()
                    .like("equipment_id", dto.getSearchContent());
        }

        queryWrapper.eq(StrUtil.isNotEmpty(dto.getStationId()), "station_id", dto.getStationId());
        queryWrapper.eq(StrUtil.isNotEmpty(dto.getEquipmentType()), "equipment_type", dto.getEquipmentType());

        queryWrapper.orderByAsc("create_time");
        page.setQueryWrapper(queryWrapper);
        page = pointEquipmentService.findPage(page);

        List<SysPointEquipment> tList = page.getTList();
        List<SysPointEquipmentVo> vList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(tList)) {
            // 场站ID
            List<String> stationIds = tList.stream().map(SysPointEquipment::getStationId).collect(Collectors.toList());
            List<SysStation> stationList = stationService.query()
                    .in("station_id", stationIds).list();

            Map<String, SysStation> stationMap = stationList.stream().collect(Collectors.toMap(SysStation::getStationId, Function.identity()));

            for (SysPointEquipment equipment : tList) {
                SysPointEquipmentVo equipmentVo = WrapperUtil.transform(equipment, SysPointEquipmentVo.class);
                //赋值场站名称和别名
                if (stationMap.containsKey(equipment.getStationId())) {
                    SysStation station = stationMap.get(equipment.getStationId());
                    equipmentVo.setStationName(station.getStationName());
                    equipmentVo.setStationAlias(station.getStationAlias());
                } else {
                    equipmentVo.setStationName("");
                    equipmentVo.setStationAlias("");
                }
                vList.add(equipmentVo);
            }

        }
        page.setVList(vList);
        return AjaxResult.success(page.getPageDataVo2());
    }

    @GetMapping("detail")
    public AjaxResult detail(@RequestParam("id") Long id) {

        SysPointEquipment pointEquipment = pointEquipmentService.getById(id);
        return AjaxResult.success(WrapperUtil.transform(pointEquipment, SysPointEquipmentVo.class));
    }


    @PostMapping("add")
    public AjaxResult add(@RequestBody SysPointEquipment pointEquipment) {

        pointEquipmentService.add(pointEquipment);
        return AjaxResult.success();
    }

    @PostMapping("update")
    public AjaxResult update(@RequestBody SysPointEquipment pointEquipment) {
        if (null == pointEquipment.getId()) {
            throw new ServiceException("校验参数失败");
        }
        pointEquipmentService.update(pointEquipment);
        return AjaxResult.success();
    }

    @GetMapping("del")
    public AjaxResult del(@RequestParam("id") Long id) {

        pointEquipmentService.del(id);
        return AjaxResult.success();
    }

    @PostMapping("batchDel")
    public AjaxResult batchDel(@RequestBody PointEquipmentDto dto) {

        pointEquipmentService.batchDel(dto.getIds());
        return AjaxResult.success();
    }

    @PostMapping("updateEquipmentId")
    public AjaxResult updateEquipmentId(@RequestBody SysPointEquipment pointEquipment) {

        pointEquipmentService.updatePointEquipment(pointEquipment);
        return AjaxResult.success();

    }
}
